package com.weixin.paleituo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baozi.treerecyclerview.adpater.TreeRecyclerAdapter;
import com.baozi.treerecyclerview.adpater.TreeRecyclerType;
import com.baozi.treerecyclerview.factory.ItemHelperFactory;
import com.baozi.treerecyclerview.item.TreeItem;
import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.AfterSaleHeadAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.AfterSaleListBean;
import com.weixin.paleituo.bean.AfterSaleRequestBean;
import com.weixin.paleituo.bean.FirstOrderListBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.item.CartGroupItem;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Author yichao
 * Time  2018/7/10 11:33
 * Dest  售后申请
 */

public class AfterSaleReques extends BaseFragment {
    @BindView(R.id.yrecycleview_)
    YRecycleview mYrecycleview;
    private TreeRecyclerAdapter treeRecyclerAdapter = new TreeRecyclerAdapter(TreeRecyclerType.SHOW_ALL);
    private LoadingPagerHead mLoadingPagerHead;
    private List<FirstOrderListBean> mFirstOrderListBeans;
    private int currentPage;
    private View mHeadView;
    private RecyclerView mHeadRecycleView;
    private List<AfterSaleListBean> mAfterSaleListBeans;
    private AfterSaleHeadAdapter mAfterSaleHeadAdapter;

    @Override
    public View initView(Context context) {

        mLoadingPagerHead = new LoadingPagerHead(context) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(mContext, R.layout.after_sale_fragment);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };


       // View contentView = UIUtils.inflate(context, R.layout.after_sale_fragment);
        mHeadView = UIUtils.inflate(context, R.layout.layout_aftersale_head);
        mHeadRecycleView = mHeadView.findViewById(R.id.recycle_layout_aftersale_head);


        return mLoadingPagerHead;
    }

    /**
     * 加载数据
     */
    private void retry() {
        currentPage=1;
        Observable<StatusCode<AfterSaleRequestBean>> firstAfterSaleRequest = ApiUtils.getApi().getFirstAfterSaleRequest(BaseApplication.sUserInfoBean.token, BaseApplication.sUserInfoBean.userId);
        HttpUtil.getInstance().toSubscribe(firstAfterSaleRequest, new ProgressSubscriber<AfterSaleRequestBean>(mContext) {
            @Override
            protected void _onNext(StatusCode<AfterSaleRequestBean> afterSaleListBeanStatusCode) {

                AfterSaleRequestBean data = afterSaleListBeanStatusCode.getData();
                List<FirstOrderListBean> firstOrderList = data.getFirstOrderList();
                if (firstOrderList!=null && firstOrderList.size()>0){
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    mFirstOrderListBeans.clear();
                    mFirstOrderListBeans.addAll(firstOrderList);
                    List<TreeItem> treeItemList = ItemHelperFactory.createTreeItemList(mFirstOrderListBeans, CartGroupItem.class, null);

                    treeRecyclerAdapter.getItemManager().replaceAllItem(treeItemList);
                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }

                List<AfterSaleListBean> afterSaleList = data.getAfterSaleList();

                if (afterSaleList!=null && afterSaleList.size()>0){

                    mAfterSaleListBeans.addAll(afterSaleList);
                    mAfterSaleHeadAdapter.notifyDataSetChanged();
                }

            }

            @Override
            protected void _onError(String message) {

                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);

            }
        },"",lifecycleSubject,false,true);


    }

    @Override
    protected void initData() {

        mAfterSaleListBeans = new ArrayList<>();
        mAfterSaleHeadAdapter = new AfterSaleHeadAdapter(mContext,mAfterSaleListBeans);
        mHeadRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mHeadRecycleView.setAdapter(mAfterSaleHeadAdapter);
        mYrecycleview.addHeadView(mHeadView);
        mYrecycleview.setLayoutManager(new LinearLayoutManager(mContext));
        mYrecycleview.setAdapter(treeRecyclerAdapter);
        mFirstOrderListBeans = new ArrayList<>();
        List<TreeItem> treeItemList = ItemHelperFactory.createTreeItemList(mFirstOrderListBeans, CartGroupItem.class, null);

        treeRecyclerAdapter.getItemManager().replaceAllItem(treeItemList);

        retry();
        mYrecycleview.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {

                Observable<StatusCode<AfterSaleRequestBean>> firstAfterSaleRequest = ApiUtils.getApi().getFirstAfterSaleRequest(BaseApplication.sUserInfoBean.token, BaseApplication.sUserInfoBean.userId);
                HttpUtil.getInstance().toSubscribe(firstAfterSaleRequest, new ProgressSubscriber<AfterSaleRequestBean>(mContext) {
                    @Override
                    protected void _onNext(StatusCode<AfterSaleRequestBean> afterSaleListBeanStatusCode) {
                        mYrecycleview.setReFreshComplete();
                        AfterSaleRequestBean data = afterSaleListBeanStatusCode.getData();
                        List<FirstOrderListBean> firstOrderList = data.getFirstOrderList();
                        if (firstOrderList!=null && firstOrderList.size()>0){
                            ToastUtils.makeText("刷新成功");
                            currentPage=1;
                            mFirstOrderListBeans.clear();
                            mFirstOrderListBeans.addAll(firstOrderList);
                            List<TreeItem> treeItemList = ItemHelperFactory.createTreeItemList(mFirstOrderListBeans, CartGroupItem.class, null);

                            treeRecyclerAdapter.getItemManager().replaceAllItem(treeItemList);

                        }else {
                            ToastUtils.makeText("刷新失败");
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.makeText("刷新失败");
                        mYrecycleview.setReFreshComplete();

                    }
                },"",lifecycleSubject,false,true);


            }

            @Override
            public void onLoadMore() {
               // mYrecycleview.setloadMoreComplete();
                Observable<StatusCode<List<FirstOrderListBean>>> secondAfterSaleRequest = ApiUtils.getApi().getSecondAfterSaleRequest(BaseApplication.sUserInfoBean.token, BaseApplication.sUserInfoBean.userId, currentPage+1 , Contans.rows);
                HttpUtil.getInstance().toSubscribe(secondAfterSaleRequest, new ProgressSubscriber<List<FirstOrderListBean>>(mContext) {
                    @Override
                    protected void _onNext(StatusCode<List<FirstOrderListBean>> afterSaleListBeanStatusCode) {
                        mYrecycleview.setloadMoreComplete();
                        List<FirstOrderListBean> data = afterSaleListBeanStatusCode.getData();

                        if (data!=null && data.size()>0){
                            currentPage+=1;

                           // mFirstOrderListBeans.addAll(data);
                            List<TreeItem> treeItemList = ItemHelperFactory.createTreeItemList(data, CartGroupItem.class, null);
                            treeRecyclerAdapter.getItemManager().addItems(treeItemList);
                        }else {
                            ToastUtils.makeText("没有更多数据了");
                        }
                    }
                    @Override
                    protected void _onError(String message) {
                        ToastUtils.makeText("加载更多失败");
                        mYrecycleview.setloadMoreComplete();

                    }
                },"",lifecycleSubject,false,true);






            }
        });

    }


    public static AfterSaleReques newInstance() {

        Bundle args = new Bundle();

        AfterSaleReques fragment = new AfterSaleReques();
        fragment.setArguments(args);
        return fragment;
    }

}
